<?php

declare(strict_types=1);

namespace App\Robot\Crontab;

use Carbon\Carbon;
use Hyperf\DbConnection\Db;

class ChatRecordCleanupCrontab
{
    public function execute()
    {
        // 删除聊天记录
        $batchSize = 1000;
        $oneMonthAgo = Carbon::now()->subMonth()->timestamp;
        do {
            $affectedRows = Db::table('ch_chat_records')
                ->where('send_time', '<', $oneMonthAgo)
                ->limit($batchSize)
                ->delete();

            if ($affectedRows > 0) {
                \Hyperf\Coroutine\Coroutine::sleep(0.5); // 休眠100毫秒
            }

        } while ($affectedRows > 0);

        return "聊天记录清理完毕";
    }
}